package com.igeek.health.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.igeek.health.constant.MessageConstant;
import com.igeek.health.entity.Member;
import com.igeek.health.entity.Order;
import com.igeek.health.entity.Ordersetting;
import com.igeek.health.mapper.MemberMapper;
import com.igeek.health.mapper.OrdersMapper;
import com.igeek.health.mapper.OrdersettingMapper;
import com.igeek.health.result.Result;
import com.igeek.health.service.IOrdersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fengqq
 * @since 2024-08-26
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersMapper, Order> implements IOrdersService {

    @Autowired
    private OrdersettingMapper ordersettingMapper;
    @Autowired
    private MemberMapper memberMapper;

    //体检预约
    @Transactional
    @Override
    public Result submit(Map<String, Object> map) {
        //1、检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有设置则无法进行预约
        Date orderDate = DateUtil.parse(map.get("orderDate")+"","yyyy-MM-dd");
        LambdaQueryWrapper<Ordersetting> orderDatequeryWrapper = new LambdaQueryWrapper<Ordersetting>().eq(Ordersetting::getOrderdate, orderDate);
        List<Ordersetting> ordersettingList = ordersettingMapper.selectList(orderDatequeryWrapper);
        if(CollectionUtil.isEmpty(ordersettingList)){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }

        //2、检查用户所选择的预约日期是否已经约满，如果已经约满则无法预约
        Ordersetting ordersetting = ordersettingList.get(0);
        //总可预约数量
        Integer number = ordersetting.getNumber();
        //已预约数量
        Integer reservations = ordersetting.getReservations();
        if(number<=reservations){
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        //4、检查当前用户是否为会员，如果是会员则直接完成预约，如果不是会员则自动完成注册并进行预约
        String telephone = map.get("telephone").toString();
        String name = map.get("name").toString();
        String sex = map.get("sex").toString();
        Integer setmealId = Integer.valueOf(map.get("setmealId").toString());
        String idCard = map.get("idCard").toString();
        List<Member> memberList =
                memberMapper.selectList(new LambdaQueryWrapper<Member>().eq(Member::getPhoneNumber, telephone));
        Member member = null;
        if(CollectionUtil.isEmpty(memberList)){
            //如果不是会员则自动完成注册
            String fileNumber = UUID.randomUUID().toString().replaceAll("-","");
            member = new Member(name, sex, idCard, telephone, new Date());
            member.setFileNumber(fileNumber);
            memberMapper.insert(member);
        }else{
            //如果是会员则直接完成预约
            member = memberList.get(0);
        }

        //3、检查用户是否重复预约（同一个用户在同一天预约了同一个套餐），如果是重复预约则无法完成再次预约
        //select * from orders where setmeal_id=? and orderDate=? and idCard=? and member_id=?
        LambdaQueryWrapper<Order> orderLambdaQueryWrapper = new LambdaQueryWrapper<>();
        orderLambdaQueryWrapper.eq(Order::getSetmealId,setmealId);
        orderLambdaQueryWrapper.eq(Order::getOrderDate,orderDate);
        orderLambdaQueryWrapper.eq(Order::getIdCard,idCard);
        orderLambdaQueryWrapper.eq(Order::getMemberId,member.getId());
        List<Order> orderList = baseMapper.selectList(orderLambdaQueryWrapper);
        if(CollectionUtil.isNotEmpty(orderList)){
            return new Result(false,MessageConstant.HAS_ORDERED);
        }

        // 5、预约成功，更新当日的已预约人数
        Order order = new Order(
                member.getId(),
                orderDate,
                map.get("orderType").toString(),
                map.get("orderStatus").toString(),
                setmealId,
                idCard);
        baseMapper.insert(order);
        //6.更新当日已经预约人数
        ordersetting.setReservations(reservations+1);
        ordersettingMapper.update(ordersetting,orderDatequeryWrapper);
        return new Result(true,MessageConstant.ORDER_SUCCESS);
    }
}
